#######################################################################################################################
import sys
import os
import json
import misp


#######################################################################################################################
class code_config_t:
	def __init__(self, name, cflags, ldflags):
		self.name = name
		self.cflags = cflags
		self.ldflags = ldflags


#######################################################################################################################
convtext_name = 'tool\\convtext.py'

text_name_list = [ \
					'data\\text\\welcome.txt',
					'data\\text\\goodbye.txt',
					]
c_name_list = [ \
					'code\\core\\core.c',
					'code\\game\\game.c',
					]
gendir_name = 'code\\gen'
outdir_name = 'output'
exe_name = 'game.exe'
code_configs = [ \
			code_config_t(name='debug', cflags='-DDEBUG -O0 -g -Wall -Werror', ldflags='-g'),
			code_config_t(name='release', cflags='-O2', ldflags=''),
			]


#######################################################################################################################
# read command line
params = json.loads(sys.argv[1])
misptemp_name = params['misptemp_name']
mispfile_name = params['mispfile_name']

# init misp data
misp = misp.misp_t()

# configs
misp.add_config(id='clean')
misp.add_config(id='data')
code_config_names = []
for code_config in code_configs:
	code_config_names.append(code_config.name)
	misp.add_config(id=code_config.name)
misp.add_config(id='all')

# clean
misp.add_phase(id='clean all', configs=['clean'])
misp.add_formula(id = 'clean', 
						actions = [
							['-s', 'del %s' % mispfile_name],
							['-s', 'rd /s /q %s' % gendir_name],
							['-s', 'rd /s /q %s' % outdir_name],
							])

# gen dir
misp.add_phase(id='create %s dir'%gendir_name, configs=code_config_names+['data','all'])
misp.add_formula(id=gendir_name, targets = [gendir_name], actions = [['-s', 'md %s' % gendir_name]])

# data texts
misp.add_phase(id='convert text files', configs=code_config_names+['data','all'])
for text_name in text_name_list:
	name = os.path.split(text_name)[1]
	name = os.path.splitext(name)[0]
	h_name = os.path.join(gendir_name, name+'.h')
	c_name = os.path.join(gendir_name, name+'.c')
	misp.add_formula(id=c_name, sources=[convtext_name, text_name], targets = [h_name, c_name],
							actions = [['','python -u %s %s %s %s' % (convtext_name, text_name, h_name, c_name)]])
	c_name_list.append(c_name)

# code configs
for code_config in code_configs:
	# out dir
	config_outdir_name = os.path.join(outdir_name, code_config.name)
	misp.add_phase(id='create %s dir'%config_outdir_name, configs=[code_config.name,'all'])
	misp.add_formula(id=config_outdir_name, targets=[config_outdir_name], actions=[['-s','md %s'%config_outdir_name]])
	# compile
	o_name_list = []
	o_name_str = ''
	misp.add_phase(id='compile %s'%code_config.name, configs=[code_config.name,'all'])
	for c_name in c_name_list:
		name = os.path.split(c_name)[1]
		name = os.path.splitext(name)[0]
		o_name = os.path.join(config_outdir_name, name + '.o')
		d_name = os.path.join(config_outdir_name, name + '.d')
		misp.add_formula(id=o_name, sources=[c_name], targets=[o_name],
								actions=[['','gcc -Icode %s -MMD -c %s -o %s'%(code_config.cflags, c_name, o_name)]],
								extra_sources_name=d_name)
		o_name_list.append(o_name)
		o_name_str += o_name+' '
	# link
	misp.add_phase(id='link %s'%code_config.name, configs=[code_config.name,'all'])
	config_exe_name = os.path.join(config_outdir_name, exe_name)
	misp.add_formula(id=config_exe_name, sources=o_name_list, targets = [config_exe_name],
							actions=[['','gcc %s %s -o %s'%(code_config.ldflags, o_name_str, config_exe_name)]])

# save
misp.write_misptemp(misptemp_name)
